package com.codeoss.framework.controller;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.codeoss.framework.response.MainResponse;
import com.codeoss.framework.service.IMainService;

@Controller
@RequestMapping("main")
public class MainController {
	@Autowired
	private IMainService mainService;
	
	@RequestMapping(value = "init", method = RequestMethod.GET)
	@ResponseBody
	public MainResponse init(){
		MainResponse mainResponse=mainService.init();
		return mainResponse;
	}
	
	@RequestMapping(value = "getPageCodeByMenuId", method = RequestMethod.GET)
	@ResponseBody
	public MainResponse getPageCodeByMenuId(@RequestParam("menuId") Long menuId){
		MainResponse mainResponse=mainService.getPageCodeByMenuId(menuId);
		return mainResponse;
	}
	
	@RequestMapping(value = "getAvatar", method = RequestMethod.GET)
	public void getAvatar(HttpServletResponse response)throws Exception{
		MainResponse mainResponse=mainService.getAvatar();
		if(mainResponse.getAvatar()!=null) {
			response.getOutputStream().write(mainResponse.getAvatar());
		}	
	}
	
	@RequestMapping(value = "changePassword", method = RequestMethod.POST)
	@ResponseBody
	public MainResponse changePassword(@RequestParam("oldPassword") String oldPassword,@RequestParam("newPassword") String newPassword,
			@RequestParam("newPassword2") String newPassword2){
		MainResponse mainResponse=mainService.changePassword(oldPassword,newPassword,newPassword2);
		return mainResponse;
	}
	
	@RequestMapping(value = "getMenuIconContent", method = RequestMethod.GET)
	@ResponseBody
	public  void getMenuIconContent(@RequestParam("menuId") Long menuId,HttpServletResponse response)throws Exception{
		MainResponse mainResponse=mainService.getMenuIconContent(menuId);
		if(mainResponse.getMenuIconContent()!=null) {
			response.getOutputStream().write(mainResponse.getMenuIconContent());
		}	
	}
	
	@RequestMapping(value = "onApplicationChangeHandler", method = RequestMethod.GET)
	@ResponseBody
	public MainResponse onApplicationChangeHandler(@RequestParam("applicationId") Long applicationId){
		MainResponse mainResponse=mainService.onApplicationChangeHandler(applicationId);
		return mainResponse;
	}
	

	@RequestMapping(value = "logout", method = RequestMethod.POST)
	@ResponseBody
	public MainResponse logout(){
		MainResponse mainResponse=mainService.logout();
		return mainResponse;
	}
}
